package com.gt.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gt.constant.MessageConstant;
import com.gt.constant.PasswordConstant;
import com.gt.exception.*;
import com.gt.mapper.BorrowMapper;
import com.gt.mapper.EmployeeMapper;
import com.gt.pojo.PageResult;
import com.gt.pojo.Result;
import com.gt.pojo.dto.EmployeeDTO;
import com.gt.pojo.dto.EmployeeLoginDTO;
import com.gt.pojo.dto.EmployeePageQueryDTO;
import com.gt.pojo.entity.Borrow;
import com.gt.pojo.entity.Employee;
import com.gt.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private BorrowMapper borrowMapper;

    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();
        Employee employee = employeeMapper.getByUsername(username);
        if (employee == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if (!password.equals(employee.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        return employee;
    }

    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        employee.setRole("USER");
        employee.setPassword(PasswordConstant.DEFAULT_PASSWORD);
        employee.setCreateTime(LocalDateTime.now());
        employeeMapper.insert(employee);
    }

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());

        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    @Override
    public Employee getById(Long id) {
        Employee employee = employeeMapper.getById(id);
        employee.setPassword("******");
        return employee;
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        String phone = employeeDTO.getPhone();
        if (phone.length() != 11){
            throw new PhoneIsInvalidException(MessageConstant.PHONE_IS_INVALID);
        }
        BeanUtils.copyProperties(employeeDTO,employee);
        employee.setUpdateTime(LocalDateTime.now());
        employeeMapper.update(employee);
    }

    @Override
    public void deleteById(Long id) {
        Employee employee = employeeMapper.getById(id);
        if (employee.getRole().equals("ADMIN") ){
          throw new ManagerNotDeleteException("管理员不能删除");
        }
        List<Borrow> borrow = borrowMapper.selectByUserId(id);
        if (borrow.size() >0){
            throw new UserAreadyBorrowException(MessageConstant.USE_RLREADY_BORROW);
        }
        employeeMapper.deleteById(id);
    }
}
